package com.data.unityplugin_market.network

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class NetWorkConfig private constructor(){

    private val DEFAULT_TIMEOUT : Long = 15;
    /*
     * base URL
     */
    private val BASE_URL :String  ="http://gank.io/api"

    var retrofit:Retrofit = initRetrofit();



    private object  Holder {
        val INSTANCE = NetWorkConfig()
    }

    companion object {
        val instance : NetWorkConfig by lazy {
            Holder.INSTANCE;
        }
    }

    private fun initRetrofit():Retrofit{
        val interceptor = LoggingInterceptor()

        val client = OkHttpClient.Builder().addInterceptor(interceptor)
                .retryOnConnectionFailure(true)
                .connectTimeout(DEFAULT_TIMEOUT,TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT,TimeUnit.SECONDS).build()

        return Retrofit.Builder().baseUrl(BASE_URL + "/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

    }

}